home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
news
/
readers
/
skim-0.8
/
skim-0
/
skim-0.8.4
/
MemAlloc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-18
|
4KB
|
157 lines
/*
* NAME
* MemAlloc.c
* DESCRIPTION
* Routines for memory allocation with simple exception handling.
* COPYRIGHT
* MemAlloc - Memory allocation routines with builtin error handling.
* Copyright (C) 1996 Rene W.J. Pijlman
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* VERSION
* $Header: /home/rene/sys/CVS_MasterSourceRepository/skim/MemAlloc.c,v 1.5 1996/02/16 23:10:48 rene Exp $
* Distributed with Skim version 0.8.4.
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "MemAlloc.h"
#define CLASS_NAME "Mem"
#include "CheckForDebris.h"
static void Terminate( size_t Size )
{
fprintf( stderr,
"Cannot allocate %u bytes. Terminating. Add RAM or increase swap space.\n",
Size );
#ifdef NDEBUG
exit( EXIT_FAILURE );
#else
/* Force a core dump for debugging. */
abort();
#endif
}
/*
* DESCRIPTION
* Return a pointer to uninitialized memory of Size bytes. The
* pointer is properly aligned for any datatype.
* REQUIRE
* ENSURE
* ( Size == 0 && Result == NULL ) || Result != NULL;
*/
void * MemAlloc( size_t Size )
{
void * Result = NULL;
if ( Size > 0 )
{
Result = malloc( Size );
IncrementObjectCount();
if ( Result == NULL )
{
Terminate( Size );
}
}
assert( ( Size == 0 && Result == NULL ) || Result != NULL );
return Result;
}
/*
* DESCRIPTION
* If OldAdress is non-NULL, the size of the object pointed to by
* OldAddress is changed to NewSize. Otherwise, NewSize bytes are
* allocated. A pointer to the properly aligned new space is returned.
*
* The contents, if any, will be unchanged up to the minimum of the old
* and the new sizes. If the new space is larger, the new space is
* unitialized.
* REQUIRE
* ENSURE
* ( NewSize == 0 && Result == NULL ) || Result != NULL;
*/
void * MemRealloc( void * OldAddress, size_t NewSize )
{
void * Result;
if ( NewSize > 0 )
{
if ( OldAddress != NULL )
{
Result = realloc( OldAddress, NewSize );
}
else
{
Result = malloc( NewSize );
IncrementObjectCount();
}
if ( Result == NULL )
{
Terminate( NewSize );
}
}
else
{
if ( OldAddress != NULL )
{
free( OldAddress );
DecrementObjectCount();
}
Result = NULL;
}
assert( ( NewSize == 0 && Result == NULL ) || Result != NULL );
return Result;
}
/*
* DESCRIPTION
* If Address is non-NULL, deallocates the space pointed to by Address.
* REQUIRE
* ENSURE
*/
void MemFree( void * Address )
{
if ( Address != NULL )
{
free( Address );
DecrementObjectCount();
}
}
char * MemNewString( const char * Original )
{
char * New = (char *)MemAlloc( strlen(Original) + 1 );
strcpy( New, Original );
return New;
}